package com.fx.controller;

import com.alibaba.fastjson.JSONObject;

import com.fx.entity.SysRes;
import com.fx.service.SysResService;
import com.fx.util.*;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sysRes")
public class SysResController extends BaseController {

    @Autowired
    SysResService sysResService;
    private static String pageTitle = "资源管理";
    private static String pageUrl = "/sysRes";


    @RequestMapping()
    @RequiresRoles(value = {"admin"})
    public String intoView(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle, Constant.LIST_TITLE));
        return "sysRes/list";
    }

    @RequestMapping("/listData")
    @ResponseBody
    public JSONObject listData(@RequestParam Map<String, Object> param) {
        Query query = new Query(param);
        List<SysRes> data = sysResService.listData(query);
        int count = sysResService.getCount(param);
        return renderPage(count, data);
    }

    @RequestMapping("/add")
    public String add(Model model) {
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle,Constant.ADD_TITLE));
        return "sysRes/add";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id,Model model){
        model.addAttribute("sysRes",sysResService.getById(id));
        model.addAttribute("pageInfo", PageInfo.getInstance(pageUrl,pageTitle,Constant.EDIT_TITLE));
        return "sysRes/edit";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public R saveOrUpdate(SysRes sysRes) {
        String flId = sysRes.getFlId();
        int result = 0;
        if (flId == null || "".equals(flId)) {
            result = sysResService.save(sysRes);
        } else {
            result = sysResService.updateById(sysRes);
        }
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/batchDel")
    @ResponseBody
    public R batchDel(@RequestParam("ids") String[] ids) {
        int result = 0;
        result = sysResService.batchDelByIds(ids);
        if (result != 0) {
            return R.ok("操作成功!");
        }
        return R.error("操作失败!");
    }

    @RequestMapping("/treeView")
    public String treeView() {
        return "sysRes/treeView";
    }

    @RequestMapping("/treeData")
    @ResponseBody
    public Tree<SysRes> treeData() {
        return sysResService.getTree();
    }

    @RequestMapping("/initSelect")
    @ResponseBody
    public List<SysRes> initSelect() {
        List<SysRes> sysResList = sysResService.getAllRes();
        return sysResList;
    }

    @RequestMapping("/iconView")
    public String iconView() {
        return "sysRes/iconView";
    }
}
